/////////////////////////////////////////////////////////////////////////
// File:        UpdatesChecker.h
// Purpose:     Class which checks in a oficial server for new versions
//              of the application, of the pass servers, of the url of
//              the pass checker.
// Author:      Tukero
// Modified by: 
// Copyright:   (c) Tukero <tukero@gmail.com>
// Licence:     GNU/LGPL v3
/////////////////////////////////////////////////////////////////////////

#ifndef _UPDATESCHECKER_H_
#define _UPDATESCHECKER_H_

#ifndef WX_PRECOMP
#  include <wx/wx.h>
#else
#  include <wx/wxprec.h>
#endif

#include <wx/regex.h>            //regular expressions to make our life more confortable
#include <wx/file.h>             //files management (for debug)
#include <wx/tokenzr.h>          //tokens to String management
#include <wx/txtstrm.h>
#include <wx/platinfo.h>
#include <wx/datetime.h>

#include "EsetInfo.h"
#include "NewVersionMsg.h"

class UpdatesChecker : public wxThread
{
private:
    bool        m_isNewServersAvailable;    
    bool        m_checkPassVerifier;
    bool        m_checkNewServers;
    wxString    m_newPassServers;
    wxString    m_newPassServersVersion;
protected:
    void      **m_endIndicator;
    bool        m_checkNewVersions;
    bool            m_isNewVersionAvailable;
    bool            m_ProcessNewVersionMsg;
    bool*           m_NewVersionIndicator;
    EsetInfo*       m_EsetInfo;
    NewVersionMsg*  m_NewVersionMsg;
    unsigned int    m_Actions2Do;
    unsigned int    m_ActionsDone;
    bool            m_ContinueReading;
public:
    enum
    {
        CHECK_SERVERS = 1,
        CHECK_APPVERSION = 2,
        CHECK_ESETSERVER = 4,
        CHECK_ALL = CHECK_SERVERS | CHECK_APPVERSION | CHECK_ESETSERVER
    };
    void SetProcessNewVersionMsg(bool value);    
    void SetNewVersionIndicator(bool *indicator)
    {
        m_NewVersionIndicator = indicator;
    }
    
    UpdatesChecker(NewVersionMsg* newVersionMsg, EsetInfo *esetInfo, wxThreadKind kind = wxTHREAD_JOINABLE);
    UpdatesChecker(unsigned int actions2Do, NewVersionMsg* newVersionMsg, EsetInfo *esetInfo, wxThreadKind kind = wxTHREAD_JOINABLE);
    //UpdatesChecker(unsigned int actions2Do, EsetInfo *esetInfo, wxThreadKind kind = wxTHREAD_JOINABLE);

    virtual void LoadActions2Do();
    virtual void ProcessNewVersionMsg();
    static void ProcessNewVersionMsg(NewVersionMsg *newVersionMsg);
    virtual void ConnectToServer();

    virtual void ProcessTagsLine(const wxString &lineOfData);
    virtual void ProcessExpirationCheckLine(const wxString &lineOfData);
    virtual void ProcessEndUpdatesLine(const wxString& WXUNUSED(lineOfData))
    {
        m_ContinueReading = false;
    }

    virtual void ProcessUnknownLine(const wxString & WXUNUSED(lineOfData)) { }
    virtual void ProcessReplacesLine(const wxString &lineOfData);
    virtual void ProcessTnodVersionLine(const wxString &lineOfData);
    virtual void ProcessLines2SkipXmlLine(const wxString &lineOfData);
    virtual void ProcessPassCheckerV1Line(const wxString &lineOfData);
    virtual void ProcessPassCheckerV2Line(wxString &lineOfData);
    virtual void ProcessPassServersVersionLine(const wxString &lineOfData );
    virtual void ProcessPassServersLine(const wxString &lineOfData );    
    virtual void ProcessTnodEndLine(const wxString& WXUNUSED(lineOfData));
    virtual void ProcessParserPartsLine(const wxString &lineOfData);

    void    SaveFoundServers();
    
    void * Entry();
    void SetEndIndicator(void** indicator)
    {
        m_endIndicator = indicator;
    }

    void OnExit()
    {
        if ( m_endIndicator != NULL )
            *m_endIndicator = NULL;
    }
    
};

#endif